/***************************************************************************
 * NSolidLink.Core -- Copyright Bit Armory Labs 2007
 *
 * FILE:     NSolidLinkConfigFile.cs, 16 code lines, [236 Bytes]
 * DATE:     8/27/2007 6:54 AM
 * MODIFIED: 8/29/2007 2:20 AM
 * PATH:     C:\Programming\Projects\Slinks\Slinks.Core
 *
 * NAMESPACE: NSolidLink.Core
 * CLASS:     NSolidLinkConfigFile
 *
 * AUTHOR:   Brian Chavez (Cowboy), bchavez@bitarmory.com
 *
 *                        Licence
 *
 * This library, code generator, and source code
 * is licensed under the terms and conditions defined in the GNU Public
 * License. You are free to use it for non-commercial purposes.
 * If you are going to use any part of it in a commercial 
 * application, contact me and we can work something out.
 * You may use the code generated output of Slink and Slink
 * as a development tool in both commercial and 
 * non-commercial applications.
 *
 * THE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS", WITHOUT WARRANTY
 * OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THIS
 * SOFTWARE.
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Collections;
using Slink.Core;
using System.Reflection;

namespace Slink.Core
{
    public class SlinkConfigFile
    {
        public const string ConfigFileName = @"web.config";

        private string webDir;

        private SlinkConfigSection configSection = null;

        /// <summary>
        /// Initializes a new instance of the NSolidLinkConfigFile class.
        /// </summary>
        /// <param name="webDir"></param>
        public SlinkConfigFile( string webDir )
        {
            this.webDir = webDir;

            if( SlinkConfigFile.Exists( this.webDir ))
            {
                this.ParseConfigFile();
            }   
        }


        /// <summary>
        /// Config section exists
        /// </summary>
        /// <returns>Bool</returns>
        private bool ConfigSectionExists
        {
            get
            {
                return this.configSection != null;
            }
        }

        /// <summary>
        /// Parse config file
        /// </summary>
        private void ParseConfigFile()
        {
            //http://blogs.covarius.com/PermaLink,guid,4b1fe1b7-883e-444b-92f9-f8c4758d6c79.aspx
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = Path.Combine(this.webDir, SlinkConfigFile.ConfigFileName);
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration( fileMap, ConfigurationUserLevel.None );
            
            try
            {
                this.configSection = config.GetSection( "Slink" ) as SlinkConfigSection;
            }
            catch( ConfigurationErrorsException e )
            {
                
            }
        }

        /// <summary>
        /// Refresh config
        /// </summary>
        public void RefreshConfig()
        {
            this.ParseConfigFile();
        }


        public bool EngineEnabled
        {
            get
            {
                if( this.ConfigSectionExists )
                {
                    return configSection.GlobalSettings.EngineEnabled;
                }
                else
                {
                    return false;
                }
            }
        }


        public string RootNamespace
        {
            get
            {
                if( this.ConfigSectionExists )
                {
                    return this.configSection.GlobalSettings.RootNamespace;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// NSolid link generated file target
        /// </summary>
        /// <returns>String</returns>
        public string TypesafeGenerateFileTarget
        {
            get
            {
                if( this.ConfigSectionExists )
                {
                    return this.configSection.GlobalSettings.TypesafeGeneratedFileTarget;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Namespace rules
        /// </summary>
        /// <returns>ICollection</returns>
        public ICollection NamespaceRules
        {
            get
            {
                if( this.ConfigSectionExists )
                {
                    return this.configSection.NamespaceRules;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Exists
        /// </summary>
        /// <param name="webDir">Web dir</param>
        /// <returns>Bool</returns>
        public static bool Exists( string webDir )
        {
            return File.Exists( Path.Combine( webDir.ToLower(), SlinkConfigFile.ConfigFileName ) );
        }
    }




    //public class NamespaceRuleCollection : ConfigurationElementCollection
    //{

    //    protected override ConfigurationElement CreateNewElement()
    //    {
    //        return new NamespaceRuleElement();
    //    }

    //    protected override object GetElementKey( ConfigurationElement element )
    //    {
    //        return ((NamespaceRuleElement)element).Extension;
    //    }

    //    public override ConfigurationElementCollectionType CollectionType
    //    {
    //        get
    //        {
    //            return ConfigurationElementCollectionType.AddRemoveClearMap;
    //        }
    //    }

    //    /// <summary>
    //    /// Item
    //    /// </summary>
    //    /// <param name="index">Index</param>
    //    /// <returns>Namespace rule element</returns>
    //    public NamespaceRuleElement this[int index]
    //    {
    //        get
    //        {
    //            return this.BaseGet( index ) as NamespaceRuleElement;
    //        }
    //        set
    //        {
    //            if( this.BaseGet( index ) != null )
    //            {
    //                this.BaseRemove( index );
    //            }
    //            this.BaseAdd( index, value );
    //        }
    //    }

    //    /// <summary>
    //    /// Item
    //    /// </summary>
    //    /// <param name="name">Name</param>
    //    /// <returns>Namespace rule element</returns>
    //    new public NamespaceRuleElement this[string name]
    //    {
    //        get
    //        {
    //            return this.BaseGet( name ) as NamespaceRuleElement;
    //        }
    //    }

    //    /// <summary>
    //    /// Index of
    //    /// </summary>
    //    /// <param name="nsRule">Ns rule</param>
    //    /// <returns>Int</returns>
    //    public int IndexOf( NamespaceRuleElement nsRule )
    //    {
    //        return this.BaseIndexOf( nsRule );
    //    }

    //    /// <summary>
    //    /// Add
    //    /// </summary>
    //    /// <param name="nsRule">Ns rule</param>
    //    public void Add( NamespaceRuleElement nsRule )
    //    {
    //        this.BaseAdd( nsRule );
    //    }

    //    protected override void BaseAdd( ConfigurationElement element )
    //    {
    //        this.BaseAdd( element, false );
    //    }

    //    /// <summary>
    //    /// Remove
    //    /// </summary>
    //    /// <param name="nsRule">Ns rule</param>
    //    public void Remove( NamespaceRuleElement nsRule )
    //    {
    //        if( this.BaseIndexOf( nsRule ) >= 0 )
    //        {
    //            this.BaseRemove( nsRule );
    //        }
    //    }

    //    /// <summary>
    //    /// Remove at
    //    /// </summary>
    //    /// <param name="index">Index</param>
    //    public void RemoveAt( int index )
    //    {
    //        this.BaseRemoveAt( index );
    //    }
    //    /// <summary>
    //    /// Remove
    //    /// </summary>
    //    /// <param name="name">Name</param>
    //    public void Remove( string name )
    //    {
    //        this.BaseRemove( name );
    //    }
    //    /// <summary>
    //    /// Clear
    //    /// </summary>
    //    public void Clear()
    //    {
    //        this.BaseClear();
    //    }
    //}


    //public class NamespaceRuleElement : ConfigurationElement
    //{
    //    private string extension;

    //    [ConfigurationProperty( "Extension", IsRequired = true, IsKey = true )]
    //    public string Extension
    //    {
    //        get
    //        {
    //            return extension;
    //        }
    //        set
    //        {
    //            extension = value;
    //        }
    //    }

    //    private bool useFolderNamespace;

    //    [ConfigurationProperty( "UseFolderNamespace", DefaultValue = true, IsRequired = false )]
    //    public bool UseFolderNamespace
    //    {
    //        get
    //        {
    //            return useFolderNamespace;
    //        }
    //        set
    //        {
    //            useFolderNamespace = value;
    //        }
    //    }


    //    public NamespaceRuleElement()
    //    {
    //    }

    //    /// <summary>
    //    /// Initializes a new instance of the NamespaceRuleElement class.
    //    /// </summary>
    //    /// <param name="extension"></param>
    //    /// <param name="useFolderNamespace"></param>
    //    public NamespaceRuleElement( string extension, bool useFolderNamespace )
    //    {
    //        this.extension = extension;
    //        this.useFolderNamespace = useFolderNamespace;
    //    }
    //}


    //public class NamespaceRulesSection : System.Configuration.ConfigurationSection
    //{

    //    [ConfigurationProperty( "NamespaceRules", IsDefaultCollection = false )]
    //    [ConfigurationCollection( typeof( NamespaceRuleCollection ),
    //                              AddItemName = "NamespaceRule",
    //                              ClearItemsName = "Clear",
    //                              RemoveItemName = "RemoveNamespaceRule" )]
    //    public NamespaceRuleCollection NamespaceRules
    //    {
    //        get
    //        {
    //            NamespaceRuleCollection namespaceRulesCollection = base["NamespaceRules"] as NamespaceRuleCollection;
    //            return namespaceRulesCollection;
    //        }
    //    }
    //}


}
